home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFskyedit / c / SFSSaveBox < prev    next >
Encoding:
Text File  |  2003-10-16  |  5.9 KB  |  172 lines

  1. /*
  2.  *  SFskyedit - Star Fighter 3000 sky colours editor
  3.  *  SkyCols savebox
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26.  
  27. /* RISC OS library files */
  28. #include "wimp.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "wimplib.h"
  32. #include "saveas.h"
  33. #include "dcs.h"
  34.  
  35. /* My library files */
  36. #include "err.h"
  37. #include "msgtrans.h"
  38. #include "Macros.h"
  39. #include "SFformats.h"
  40. #include "FilePerc.h"
  41. #include "Loader.h"
  42.  
  43. /* Local headers */
  44. #include "EditSky.h"
  45. #include "SFSSaveBox.h"
  46. #include "utils.h"
  47. #include "DCS_dialogue.h"
  48.  
  49. ObjectId savebox_sharedid = NULL_ObjectId;
  50.  
  51. /* ----------------------------------------------------------------------- */
  52. /*                       Function prototypes                               */
  53.  
  54. static ToolboxEventHandler _SaveFile_savecompleted, _SaveFile_savehandler, _SaveFile_abouttoopen, _SaveFile_buttonshandler;
  55. static void set_filename(ViewData *view_data);
  56.  
  57. /* ----------------------------------------------------------------------- */
  58. /*                         Public functions                                */
  59.  
  60. void SaveFile_initialise(IdBlock *id_block)
  61. {
  62.   savebox_sharedid = id_block->self_id;
  63.  
  64.   ObjectId savebox_windowid;
  65.   EF(saveas_get_window_id(0, savebox_sharedid, &savebox_windowid));
  66.   EF(event_register_toolbox_handler(savebox_windowid, ActionButton_Selected, _SaveFile_buttonshandler, NULL));
  67.  
  68.   EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_AboutToBeShown, _SaveFile_abouttoopen, NULL));
  69.   EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_SaveCompleted, _SaveFile_savecompleted, NULL));
  70.   EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_SaveToFile, _SaveFile_savehandler, NULL));
  71. }
  72.  
  73. /* ----------------------------------------------------------------------- */
  74. /*                         Private functions                               */
  75.  
  76. static int _SaveFile_buttonshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  77. {
  78.   ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
  79.   if(id_block->self_component != 0x82bc02 || !FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
  80.     return 0; /* not interested */
  81.  
  82.   /* Cancel button clicked on underlying window - reset dialogue box */
  83.   ViewData *view_data;
  84.   ObjectId sb_ancestor;
  85.   E_RETV(toolbox_get_ancestor(0, savebox_sharedid, &sb_ancestor, NULL), 1)
  86.   if(!E(toolbox_get_client_handle(0, sb_ancestor, (void **)&view_data)))
  87.     set_filename(view_data);
  88.  
  89.   return 1; /* claim event */
  90. }
  91.  
  92. /* ----------------------------------------------------------------------- */
  93.  
  94. static int _SaveFile_abouttoopen(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  95. {
  96.   /* Set up dialogue box for ancestor document */
  97.   ViewData *view_data;
  98.   if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
  99.     set_filename(view_data);
  100.  
  101.   return 0; /* pass this event on */
  102. }
  103.  
  104. /* ----------------------------------------------------------------------- */
  105.  
  106. static int _SaveFile_savehandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  107. {
  108.   SaveAsSaveToFileEvent *sastf = (SaveAsSaveToFileEvent *)event;
  109.   ViewData *view_data;
  110.   _kernel_oserror *err;
  111.  
  112.   err = toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data);
  113.   if(err != NULL)
  114.     goto SaveFail;
  115.  
  116.   /* Save Fednet file */
  117.   err = perc_operation(FILEPERC_OP_COMP, sastf->filename, FILETYPE_SKYCOLS, (flex_ptr)&view_data->sky);
  118.   if(err != NULL) /* Saving error */
  119.     goto SaveFail;
  120.  
  121.   RE(saveas_file_save_completed(1, id_block->self_id, sastf->filename));
  122.   return 1; /* claim event */
  123.  
  124.   SaveFail:
  125.     err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
  126.     RE(saveas_file_save_completed(0, id_block->self_id, sastf->filename));
  127.     return 1; /* claim event */
  128. }
  129.  
  130. /* ----------------------------------------------------------------------- */
  131.  
  132. static int _SaveFile_savecompleted(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  133. {
  134.   SaveAsSaveCompletedEvent *sasc = (SaveAsSaveCompletedEvent *)event;
  135.   ViewData *view_data;
  136.  
  137.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  138.  
  139.   if(FLAG_SET(sasc->hdr.flags, SaveAs_DestinationSafe)) {
  140.     /* Mark data as saved */
  141.     char *buf;
  142.     if(!E(loader_canonicalise(&buf, NULL, NULL, sasc->filename))) {
  143.       EditSky_newfile(view_data, buf, true);
  144.       free(buf);
  145.     } else
  146.       EditSky_newfile(view_data, sasc->filename, true);
  147.  
  148.     /* If we were opened from the DCS dbox, close the document window */
  149.     if(id_block->parent_id == dcs_sharedid) {
  150.  
  151.       if(dcs_openparent) /* (set if ADJUST-click on document's close icon) */
  152.         EditSky_openparentdir(view_data); /* open parent directory of file */
  153.  
  154.       RE(toolbox_delete_object(0, id_block->ancestor_id))
  155.     }
  156.   }
  157.   return 1; /* claim event */
  158. }
  159.  
  160. /* ----------------------------------------------------------------------- */
  161.  
  162. static void set_filename(ViewData *view_data)
  163. {
  164.   char *filename;
  165.   if(strcmp(view_data->last_savepath, "<untitled>") == 0)
  166.     filename = "SkyCols"; /* invent sensible leafname */
  167.   else
  168.     /* use existing title (whether leafname or full path) */
  169.     filename = view_data->last_savepath;
  170.   RE(saveas_set_file_name(0, savebox_sharedid, filename))
  171. }
  172.